from websocket.msghandler import MsgContext, MessageType


class SimpleTaskIndicators:
    @classmethod
    def handle(cls, message):
        indicator_data = message['indicator_data']
        current_index = message['current_index']
        indicator_name = indicator_data['name']
        pops = MsgContext.single_populations[current_index]
        if indicator_name == 'obj' or indicator_name == 'dec':
            MsgContext.callback({
                'messageType': MessageType.SIMPLE_TASK_INDICATORS.value,
                'indicator_value': pops[-1][indicator_name].tolist()
            })
        else:
            indicator_class = MsgContext.get_cls_by_str(indicator_data['cls'])
            for pop in pops:
                indicator_value = indicator_class.get_value(pop, MsgContext.simple_problem_objs[current_index])
                MsgContext.callback({
                    'messageType': MessageType.SIMPLE_TASK_INDICATORS.value,
                    'indicator_value': [pop['FE'], indicator_value]
                })
        MsgContext.task_complete_return(MessageType.SIMPLE_TASK_COMPLETED)
